package org.benmaomao.webqq.protocol;

import java.net.HttpURLConnection;
import java.net.URL;

import org.benmaomao.webqq.resoureBundle.ResourceBundleUtility;

public class HttpProtocol{
		
	public static String sendHttpMessage(String url,String method,String contents,String cookie) throws Exception{
		URL serverURL = new URL(url);
		HttpURLConnection  conn = (HttpURLConnection) serverURL.openConnection();
		conn.setConnectTimeout(Integer.parseInt(ResourceBundleUtility.getResourceString("TIMEOUT_TIME")));
		conn.setRequestMethod(method);
		conn.addRequestProperty("Referer", ResourceBundleUtility.getResourceString("REFERER"));
		conn.addRequestProperty("Cookie", cookie);
		conn.addRequestProperty("Connection", "Keep-Alive"); 
		conn.addRequestProperty("Accept-Language", "zh-cn");  
		conn.addRequestProperty("Accept-Encoding", "gzip, deflate"); 
		conn.addRequestProperty("Cache-Control", "no-cache");  
		conn.addRequestProperty("Accept-Charset", "UTF-8;");
		conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; " +
				"Trident/4.0; .NET CLR 2.0.50727)"); 
		
		if (method.equalsIgnoreCase("GET")){
			conn.connect();
		}
		else if (method.equalsIgnoreCase("POST")){
			conn.setDoOutput(true);
			conn.connect();
			conn.getOutputStream().write(contents.getBytes());
		}
		else
			throw new RuntimeException("your method is not implemented");
		
		return null;
	}
}
